package name.huzhenbo.java.algorithm.talent;

import java.util.List;
import java.util.ArrayList;

class ContinualSubFinder {


    public void go(int n) {
        find(n, n / 2 + 1);
    }

    private void find(int n, int end) {
        if(end < 2){
            return;
        }
        int current = end;
        for (int i = end - 1; i > 0; i--) {
            current += i;
            if (current == n) {
                print(n, i, end);
            }
            // when current > n, delete the bigest one, and reduce end by 1 which means current sub start from end - 1.
            // when current > n, means [end...i].sum > n and [end...i+1] < n, which means [end-1...i] < n because end > i
            else if(current > n){
                current -= end--;
            }
        }
    }

    private void print(int n, int start, int end) {
        StringBuilder sub = new StringBuilder(new Integer(n).toString());
        sub.append(" = ");
        sub.append(new Integer(start).toString());
        while(start < end){
            start++;
            sub.append(" + ");
            sub.append(new Integer(start).toString());
        }
        System.out.println(sub);
    }

}
